home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / cfb / cfbcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-15  |  6.8 KB  |  228 lines

  1. /************************************************************
  2. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  3.  
  4.                     All Rights Reserved
  5.  
  6. Permission  to  use,  copy,  modify,  and  distribute   this
  7. software  and  its documentation for any purpose and without
  8. fee is hereby granted, provided that the above copyright no-
  9. tice  appear  in all copies and that both that copyright no-
  10. tice and this permission notice appear in  supporting  docu-
  11. mentation,  and  that the names of Sun or MIT not be used in
  12. advertising or publicity pertaining to distribution  of  the
  13. software  without specific prior written permission. Sun and
  14. M.I.T. make no representations about the suitability of this
  15. software for any purpose. It is provided "as is" without any
  16. express or implied warranty.
  17.  
  18. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  19. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  20. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  21. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  23. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  24. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  25. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26.  
  27. ********************************************************/
  28.  
  29.  
  30. #include "X.h"
  31. #include "scrnintstr.h"
  32. #include "colormapst.h"
  33. #include "resource.h"
  34.  
  35. #ifdef    STATIC_COLOR
  36.  
  37. static ColormapPtr InstalledMaps[MAXSCREENS];
  38.  
  39. int
  40. cfbListInstalledColormaps(pScreen, pmaps)
  41.     ScreenPtr    pScreen;
  42.     Colormap    *pmaps;
  43. {
  44.     /* By the time we are processing requests, we can guarantee that there
  45.      * is always a colormap installed */
  46.     *pmaps = InstalledMaps[pScreen->myNum]->mid;
  47.     return (1);
  48. }
  49.  
  50.  
  51. void
  52. cfbInstallColormap(pmap)
  53.     ColormapPtr    pmap;
  54. {
  55.     int index = pmap->pScreen->myNum;
  56.     ColormapPtr oldpmap = InstalledMaps[index];
  57.  
  58.     if(pmap != oldpmap)
  59.     {
  60.     /* Uninstall pInstalledMap. No hardware changes required, just
  61.      * notify all interested parties. */
  62.     if(oldpmap != (ColormapPtr)None)
  63.         WalkTree(pmap->pScreen, TellLostMap, (char *)&oldpmap->mid);
  64.     /* Install pmap */
  65.     InstalledMaps[index] = pmap;
  66.     WalkTree(pmap->pScreen, TellGainedMap, (char *)&pmap->mid);
  67.  
  68.     }
  69. }
  70.  
  71. void
  72. cfbUninstallColormap(pmap)
  73.     ColormapPtr    pmap;
  74. {
  75.     int index = pmap->pScreen->myNum;
  76.     ColormapPtr curpmap = InstalledMaps[index];
  77.  
  78.     if(pmap == curpmap)
  79.     {
  80.     if (pmap->mid != pmap->pScreen->defColormap)
  81.     {
  82.         curpmap = (ColormapPtr) LookupIDByType(pmap->pScreen->defColormap,
  83.                            RT_COLORMAP);
  84.         (*pmap->pScreen->InstallColormap)(curpmap);
  85.     }
  86.     }
  87. }
  88.  
  89. #endif
  90.  
  91. void
  92. cfbResolveColor(pred, pgreen, pblue, pVisual)
  93.     unsigned short    *pred, *pgreen, *pblue;
  94.     register VisualPtr    pVisual;
  95. {
  96.     int shift = 16 - pVisual->bitsPerRGBValue;
  97.     unsigned lim = (1 << pVisual->bitsPerRGBValue) - 1;
  98.  
  99.     if ((pVisual->class == PseudoColor) || (pVisual->class == DirectColor))
  100.     {
  101.     /* rescale to rgb bits */
  102.     *pred = ((*pred >> shift) * 65535) / lim;
  103.     *pgreen = ((*pgreen >> shift) * 65535) / lim;
  104.     *pblue = ((*pblue >> shift) * 65535) / lim;
  105.     }
  106.     else if (pVisual->class == GrayScale)
  107.     {
  108.     /* rescale to gray then rgb bits */
  109.     *pred = (30L * *pred + 59L * *pgreen + 11L * *pblue) / 100;
  110.     *pblue = *pgreen = *pred = ((*pred >> shift) * 65535) / lim;
  111.     }
  112.     else if (pVisual->class == StaticGray)
  113.     {
  114.     unsigned limg = pVisual->ColormapEntries - 1;
  115.     /* rescale to gray then [0..limg] then [0..65535] then rgb bits */
  116.     *pred = (30L * *pred + 59L * *pgreen + 11L * *pblue) / 100;
  117.     *pred = ((((*pred * (limg + 1))) >> 16) * 65535) / limg;
  118.     *pblue = *pgreen = *pred = ((*pred >> shift) * 65535) / lim;
  119.     }
  120.     else
  121.     {
  122.     unsigned limr, limg, limb;
  123.  
  124.     limr = pVisual->redMask >> pVisual->offsetRed;
  125.     limg = pVisual->greenMask >> pVisual->offsetGreen;
  126.     limb = pVisual->blueMask >> pVisual->offsetBlue;
  127.     /* rescale to [0..limN] then [0..65535] then rgb bits */
  128.     *pred = ((((((*pred * (limr + 1)) >> 16) *
  129.             65535) / limr) >> shift) * 65535) / lim;
  130.     *pgreen = ((((((*pgreen * (limg + 1)) >> 16) *
  131.               65535) / limg) >> shift) * 65535) / lim;
  132.     *pblue = ((((((*pblue * (limb + 1)) >> 16) *
  133.              65535) / limb) >> shift) * 65535) / lim;
  134.     }
  135. }
  136.  
  137. Bool
  138. cfbInitializeColormap(pmap)
  139.     register ColormapPtr    pmap;
  140. {
  141.     register unsigned i;
  142.     register VisualPtr pVisual;
  143.     unsigned lim, maxent, shift;
  144.  
  145.     pVisual = pmap->pVisual;
  146.     lim = (1 << pVisual->bitsPerRGBValue) - 1;
  147.     shift = 16 - pVisual->bitsPerRGBValue;
  148.     maxent = pVisual->ColormapEntries - 1;
  149.     if (pVisual->class == TrueColor)
  150.     {
  151.     unsigned limr, limg, limb;
  152.  
  153.     limr = pVisual->redMask >> pVisual->offsetRed;
  154.     limg = pVisual->greenMask >> pVisual->offsetGreen;
  155.     limb = pVisual->blueMask >> pVisual->offsetBlue;
  156.     for(i = 0; i <= maxent; i++)
  157.     {
  158.         /* rescale to [0..65535] then rgb bits */
  159.         pmap->red[i].co.local.red =
  160.         ((((i * 65535) / limr) >> shift) * 65535) / lim;
  161.         pmap->green[i].co.local.green =
  162.         ((((i * 65535) / limg) >> shift) * 65535) / lim;
  163.         pmap->blue[i].co.local.blue =
  164.         ((((i * 65535) / limb) >> shift) * 65535) / lim;
  165.     }
  166.     }
  167.     else if (pVisual->class == StaticColor)
  168.     {
  169.     unsigned limr, limg, limb;
  170.  
  171.     limr = pVisual->redMask >> pVisual->offsetRed;
  172.     limg = pVisual->greenMask >> pVisual->offsetGreen;
  173.     limb = pVisual->blueMask >> pVisual->offsetBlue;
  174.     for(i = 0; i <= maxent; i++)
  175.     {
  176.         /* rescale to [0..65535] then rgb bits */
  177.         pmap->red[i].co.local.red =
  178.         ((((((i & pVisual->redMask) >> pVisual->offsetRed)
  179.             * 65535) / limr) >> shift) * 65535) / lim;
  180.         pmap->red[i].co.local.green =
  181.         ((((((i & pVisual->greenMask) >> pVisual->offsetGreen)
  182.             * 65535) / limg) >> shift) * 65535) / lim;
  183.         pmap->red[i].co.local.blue =
  184.         ((((((i & pVisual->blueMask) >> pVisual->offsetBlue)
  185.             * 65535) / limb) >> shift) * 65535) / lim;
  186.     }
  187.     }
  188.     else if (pVisual->class == StaticGray)
  189.     {
  190.     for(i = 0; i <= maxent; i++)
  191.     {
  192.         /* rescale to [0..65535] then rgb bits */
  193.         pmap->red[i].co.local.red = ((((i * 65535) / maxent) >> shift)
  194.                      * 65535) / lim;
  195.         pmap->red[i].co.local.green = pmap->red[i].co.local.red;
  196.         pmap->red[i].co.local.blue = pmap->red[i].co.local.red;
  197.     }
  198.     }
  199.     return TRUE;
  200. }
  201.  
  202. Bool
  203. cfbCreateDefColormap(pScreen)
  204.     ScreenPtr pScreen;
  205. {
  206.     unsigned short    zero = 0, ones = ~0;
  207.     VisualPtr    pVisual;
  208.     ColormapPtr    cmap;
  209.     
  210.     for (pVisual = pScreen->visuals;
  211.      pVisual->vid != pScreen->rootVisual;
  212.      pVisual++)
  213.     ;
  214.  
  215.     if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &cmap,
  216.                (pVisual->class & DynamicClass) ? AllocNone : AllocAll,
  217.                0)
  218.     != Success)
  219.     return FALSE;
  220.     if ((AllocColor(cmap, &ones, &ones, &ones, &(pScreen->whitePixel), 0) !=
  221.               Success) ||
  222.         (AllocColor(cmap, &zero, &zero, &zero, &(pScreen->blackPixel), 0) !=
  223.               Success))
  224.         return FALSE;
  225.     (*pScreen->InstallColormap)(cmap);
  226.     return TRUE;
  227. }
  228.